package net.hn.hnms.biz.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.plan.domain.DispatchKeepWatchPerson;
import net.hn.hnms.biz.plan.mapper.DispatchKeepWatchPersonMapper;
import net.hn.hnms.biz.plan.request.DispatchKeepWatchPersonReq;
import net.hn.hnms.biz.plan.service.IDispatchKeepWatchPersonService;
import net.hn.hnms.biz.plan.vo.DispatchKeepWatchPersonVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaoyang
 * 带值班人员管理服务层实现
 * @date 2022-08-01
 */

@Service
@DS("master")
public class DispatchKeepWatchPersonServiceImpl extends BaseService<DispatchKeepWatchPerson, DispatchKeepWatchPersonVO>  implements IDispatchKeepWatchPersonService {

    @Autowired
    private DispatchKeepWatchPersonMapper dispatchKeepWatchPersonMapper;

    /**
     * @param dispatchKeepWatchPerson
     * @param pageQuery       查询带值班人员列表
     */
    @Override
    public ResponsePage<DispatchKeepWatchPersonVO> selectPageList(DispatchKeepWatchPersonReq dispatchKeepWatchPerson, PageQuery pageQuery) {

        Page<DispatchKeepWatchPerson> page = dispatchKeepWatchPersonMapper.selectPage(pageQuery.build(), buildQuery(dispatchKeepWatchPerson).orderByDesc(DispatchKeepWatchPerson::getCreateTime));

        List<DispatchKeepWatchPersonVO> list =page.getRecords().stream().map(o ->convert(o, DispatchKeepWatchPersonVO.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }


    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<DispatchKeepWatchPerson> 查询条件
     */
    private LambdaQueryWrapper<DispatchKeepWatchPerson> buildQuery(DispatchKeepWatchPersonReq req){
        return new LambdaQueryWrapper<DispatchKeepWatchPerson>()
                .eq(StrUtil.isNotBlank(req.getKeepWatchPersonPhone()), DispatchKeepWatchPerson::getKeepWatchPersonPhone, req.getKeepWatchPersonPhone())
                .eq(StrUtil.isNotBlank(req.getKeepWatchPersonName()), DispatchKeepWatchPerson::getKeepWatchPersonName, req.getKeepWatchPersonName())
                .eq(StrUtil.isNotBlank(req.getStatus()), DispatchKeepWatchPerson::getStatus, req.getStatus())
                .eq(StrUtil.isNotBlank(req.getMineCode()), DispatchKeepWatchPerson::getMineCode, req.getMineCode())
                .orderByAsc(DispatchKeepWatchPerson::getCreateTime);
    }

    /**
     *
     * @param dispatchKeepWatchPerson
     * 查询带值班人员列表
     */
    @Override
    public List<DispatchKeepWatchPersonVO> selectList(DispatchKeepWatchPersonReq dispatchKeepWatchPerson) {
        List<DispatchKeepWatchPerson> list = dispatchKeepWatchPersonMapper.selectList(buildQuery(dispatchKeepWatchPerson));
        return list.stream().map(o -> convert(o, DispatchKeepWatchPersonVO.class)).collect(Collectors.toList());
    }

    @Override
    public List<DispatchKeepWatchPersonVO> selectPostType() {
        List<DispatchKeepWatchPerson> list = dispatchKeepWatchPersonMapper.selectPostType();
        return list.stream().map(o -> convert(o, DispatchKeepWatchPersonVO.class)).collect(Collectors.toList());
    }

    /**
     * @param dispatchKeepWatchPerson 新增带值班人员
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DispatchKeepWatchPersonReq dispatchKeepWatchPerson) {
//        QueryWrapper<DispatchKeepWatchPerson> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("development_plan_code", dispatchKeepWatchPerson.getDispatchKeepWatchPersonCode());
//        if (dispatchKeepWatchPersonMapper.selectCount(queryWrapper) > 0) {
//            throw new HuaNengException(Constants.FAIL, "已存在相同的计划编号");
//        }
        DispatchKeepWatchPerson insertObj = new DispatchKeepWatchPerson();
        BeanUtil.copyProperties(dispatchKeepWatchPerson, insertObj);
        insertObj.setStatus("0");

        return dispatchKeepWatchPersonMapper.insert(insertObj);
    }

    /**
     * @param idList 批量删除 id集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        dispatchKeepWatchPersonMapper.deleteBatchIds(idList);
    }

    /**
     * @param dispatchKeepWatchPerson 单个修改 按照主键修改信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DispatchKeepWatchPersonReq dispatchKeepWatchPerson) {
        if (StrUtil.isBlank(dispatchKeepWatchPerson.getKeepWatchPersonId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        DispatchKeepWatchPerson updateObj = new DispatchKeepWatchPerson();
        BeanUtil.copyProperties(dispatchKeepWatchPerson, updateObj);
        int updateCount = dispatchKeepWatchPersonMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    /**
     * @param id 按主键id查询详情
     */
    @Override
    public DispatchKeepWatchPersonVO selectDetailById(String id) {
        DispatchKeepWatchPerson dispatchKeepWatchPerson = dispatchKeepWatchPersonMapper.selectById(id);
        if (dispatchKeepWatchPerson == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(dispatchKeepWatchPerson, DispatchKeepWatchPersonVO.class);
    }


    /**
     * 启用/禁用
     * @param id 数据id
     * @param status  status: 0:启用 1:禁用
     */
    public void changeStatus(String id, Integer status) {
        if (status == 1 || status == 0) {
            DispatchKeepWatchPerson planMining = dispatchKeepWatchPersonMapper.selectById(id);
            if (planMining == null) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            planMining.setStatus(status + "");
            int res = dispatchKeepWatchPersonMapper.updateById(planMining);
            if (res == 0) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            return;
        }
        throw new HuaNengException(Constants.FAIL, "传入的参数错误");
    }
}
